package com.ohos.carbon.slice.widgets;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.model.ComponentRatingDataBean;
import com.ohos.carbon.provider.BackdropProvider;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.ListContainer;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ElementScatter;

import java.util.ArrayList;
import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;

public class BackdropSlice extends AbilitySlice {

    private DirectionalLayout directionLayout, layoutList;
    private ListContainer listContainer;
    private Image imageDrawer;
    private ArrayList<ComponentRatingDataBean> arrayList;
    boolean isOpen = false;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_backdrop);
        initData();
        initView();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Backdrop")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Backdrop")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Backdrop", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("Backdrop", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("Backdrop", 1);
            pf.flushSync();
        }
    }

    private void initView() {
        directionLayout = (DirectionalLayout) findComponentById(ResourceTable.Id_direction_layout);
        imageDrawer = (Image) findComponentById(ResourceTable.Id_image_drawer);


        //第二个布局+列表
        layoutList = (DirectionalLayout) findComponentById(ResourceTable.Id_layout_list);
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_list_container);
        listContainer.setItemProvider(new BackdropProvider(arrayList, getContext()));

        //点击事件（类似于抽屉布局功能）
        imageDrawer.setClickedListener(new Component.ClickedListener() {

            //动画
            AnimatorProperty animator = layoutList.createAnimatorProperty();

            @Override
            public void onClick(Component component) {
                if (isOpen) {
                    animator.moveFromX(500).moveToX(160).setDuration(200);
//                    layoutList.setContentPosition(160, 0);
                    isOpen = false;
                } else {
                    animator.moveFromX(160).moveToX(500).setDuration(200);
//                    layoutList.setContentPosition(500, 0);
                    isOpen = true;
                }
                animator.start();
            }
        });

    }

    private void initData() {
        arrayList = new ArrayList<>();

        Element favour = ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_star_yellow_24dp);
        Element unFavour = ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_star_gray_24dp);

        arrayList.add(new ComponentRatingDataBean(0, null, null, null, null, null, null, null, null, null));
        arrayList.add(new ComponentRatingDataBean(ResourceTable.Media_randomdata_man3, null, "Fran Stephen Barnes", "21:48,19 Mar", "Lorem ipsum dolor sit ament,consectetur adipiscingelit.", unFavour, unFavour, unFavour, unFavour, unFavour));
        arrayList.add(new ComponentRatingDataBean(ResourceTable.Media_randomdata_woman0, null, "Victor Kevin Green", "16:57,16 Mar", "Proin nibh augue,suscipit a,scelerisque sed,laciniain,mi.", favour, favour, favour, unFavour, unFavour));
        arrayList.add(new ComponentRatingDataBean(ResourceTable.Media_randomdata_man8, null, "Steve Douglas Rivera", "00:56,19 Mar", "Cras vel lorem", unFavour, unFavour, unFavour, unFavour, unFavour));
        arrayList.add(new ComponentRatingDataBean(0, null, null, null, null, null, null, null, null, null));

        arrayList.add(new ComponentRatingDataBean(ResourceTable.Media_randomdata_background0, null, "Terry Collins", "14:56,13 Mar", "Pjinqs idnuw nkqnd nknd nidqniq okqk avcb iqsn ossm mkanx mak mzkmz mkN", favour, favour, favour, unFavour, unFavour));
        arrayList.add(new ComponentRatingDataBean(ResourceTable.Media_randomdata_man9, null, "Bhsh Aisb", "07:19,10 Mar", "Onnx nxmnck nmax mkm mlkM bhabxuh csgqycd vydqv bhxajb bajnxb hqvq  bnabx", favour, favour, favour, favour, favour));
        arrayList.add(new ComponentRatingDataBean(ResourceTable.Media_randomdata_man5, null, "Gnis Ans", "17:39,10 Mar", "Cjhbx nbmbcj jabxj nxmnbx mkmn mzmbb bhjvsb qusb bja jbs", favour, favour, favour, favour, unFavour));


        arrayList.add(new ComponentRatingDataBean(0, null, null, null, null, null, null, null, null, null));
        arrayList.add(new ComponentRatingDataBean(ResourceTable.Media_randomdata_man6, null, "khi Hjis", "10:28,23 Mar", "Zhjn knx knkqn kjmdkq smjks pkspks majka qds", unFavour, unFavour, unFavour, unFavour, unFavour));
        arrayList.add(new ComponentRatingDataBean(ResourceTable.Media_randomdata_man7, null, "Khhsn Ljis", "17:22,11 Mar", "huansod nisbid jodndibq", favour, unFavour, unFavour, unFavour, unFavour));
        arrayList.add(new ComponentRatingDataBean(0, null, null, null, null, null, null, null, null, null));


        arrayList.add(new ComponentRatingDataBean(ResourceTable.Media_randomdata_man8, null, "Vhusb Hjis", "23:19,11 Mar", "Pjinqs idnuw nkqnd nknd nidqniq okqk avcb iqsn ossm mkanx mak mzkmz mkN", favour, unFavour, unFavour, unFavour, unFavour));
        arrayList.add(new ComponentRatingDataBean(ResourceTable.Media_randomdata_man2, null, "Khhsn Vgug Ljis", "11:17,16 Mar", "Qjidh hfnhifn infcfc injkl akmdlmd jmodmsk", favour, favour, favour, favour, unFavour));
        arrayList.add(new ComponentRatingDataBean(ResourceTable.Media_randomdata_man3, null, "Sjihn Ojis", "17:22,11 Mar", "Thjd ijf ojfow ijfk jsfhj jlkdwj ijrow", favour, favour, unFavour, unFavour, unFavour));

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
